package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xinjiang.tarim.travel.mapper.RouteMapper;
import org.xinjiang.tarim.travel.model.*;
import org.xinjiang.tarim.travel.service.RouteDayService;
import org.xinjiang.tarim.travel.service.RouteMddService;
import org.xinjiang.tarim.travel.service.RouteService;
import org.xinjiang.tarim.travel.service.UserService;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.*;

@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    RouteMapper mapper;
    @Autowired
    RouteMddService routeMddService;
    @Autowired
    RouteDayService routeDayService;
    @Autowired
    UserService userService;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public Long insert(RouteWithBLOBs record, List<PoiMdd> poiMddList) {
        mapper.insert(record);
        Long routeId = record.getId();

        //行程目的地
        List insertList = new ArrayList();
        for(PoiMdd poiMdd : poiMddList){
            RouteMdd routeMdd = new RouteMdd();
            routeMdd.setRouteId(routeId);
            routeMdd.setMddId(poiMdd.getMddId());
            routeMdd.setMddName(poiMdd.getName());
            insertList.add(routeMdd);
        }
        insertList = routeMddService.insertByList(insertList);

        //行程天数安排
        Date startDayDate = record.getStartDayDate();//出发日期
        Integer dayTime = record.getDaytime();//行程天数
        List<RouteDay> routeDayList = new ArrayList();
        for(int i=1;i<=dayTime;i++){
            RouteDay routeDay = new RouteDay();
            routeDay.setRouteId(routeId);
            routeDay.setOrder(i);
            Calendar c = Calendar.getInstance();
            c.setTime(startDayDate);
            c.add(Calendar.DAY_OF_MONTH, i);
            Date tomorrow = c.getTime();
            routeDay.setDayDate(tomorrow);
            routeDayList.add(routeDay);
        }
        routeDayService.insert(routeDayList);
        return routeId;
    }

    @Override
    public RouteWithBLOBs selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Map selectByRouteId(Long id) {
        RouteWithBLOBs route = mapper.selectByPrimaryKey(id);
        List list = routeDayService.selectByRouteId(id);
        Map map = new HashMap();
        map.put("route", route);
        map.put("routeDay", list);
        return map;
    }

    @Override
    public PageBean selectByOpenid(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1)*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = mapper.selectByOpenidCount(criteria);
        List resultList = mapper.selectByOpenid(criteria);
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(resultList);
        return pageBean;
    }

    @Override
    public int updateCoverImage(RouteWithBLOBs route) {
        return mapper.updateCoverImage(route);
    }

    @Override
    public int updateByPrimaryKeySelective(RouteWithBLOBs route) {
        return mapper.updateByPrimaryKeySelective(route);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public int updateForRelease(RouteWithBLOBs route, List<RouteDay> routeDays) {
        int i = mapper.updateForRelease(route);
        int j = routeDayService.updateForRelease(routeDays);
        return i;
    }

    @Override
    public int updateForRelease(RouteWithBLOBs route) {
        return mapper.updateForRelease(route);
    }

    @Override
    public PageBean selectRecommend(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1)*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = mapper.selectRecommendCount(criteria);
        List<RouteWithBLOBs> list = mapper.selectRecommend(criteria);
        List<Map> mapList = new ArrayList<>();
        for (RouteWithBLOBs route : list) {
            Map map = new HashMap();
            User user = userService.selectByPrimaryKey(route.getOpenid());
            List mddList = routeMddService.seletcByRouteId(route.getId());
            map.put("route", route);
            map.put("user", user);
            map.put("mddList", mddList);
            mapList.add(map);
        }
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(mapList);
        return pageBean;
    }


}
